package com.weipai;

import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.ScheduledMessage;
import org.json.JSONArray;
import org.json.JSONObject;

import javax.jms.Connection;
import javax.jms.*;
import java.util.Date;

/**
 * Created by Administrator on 2017/11/18 0018.
 */
public final class Index {


    public static String broker_url = "failover:(tcp://116.62.119.148:61616)";
    private static String jiuxin_queue_name = "jiuxin.queue";

    /*
    Deliveryexecutiontime 微秒
     */
    public static Boolean generateTask(final String name, long Deliveryexecutiontime, long currentunixtime, String queue_name, String tcpurl) throws Exception {
        broker_url = tcpurl;
        final ConnectionFactory connFactory = new ActiveMQConnectionFactory(ActiveMQConnection.DEFAULT_USER, ActiveMQConnection.DEFAULT_PASSWORD, broker_url);
        final Connection conn = connFactory.createConnection();
        final Session sess = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);

        jiuxin_queue_name = queue_name;

        final Destination dest = sess.createQueue(jiuxin_queue_name);
        final MessageProducer prod = sess.createProducer(dest);


        // 消息持久化
        prod.setDeliveryMode(DeliveryMode.PERSISTENT);
//        for(int i=0;i<100000;i++){
        final Message msg = sess.createTextMessage(name);
        long ctime = new Date().getTime();
        long time = 10 * 1000;// 延时1min
        time = Deliveryexecutiontime - ctime;
        long period = 10 * 1000;// 每个10s
        int repeat = 6;// 6次
        msg.setLongProperty(ScheduledMessage.AMQ_SCHEDULED_DELAY, time);
        //  msg.setLongProperty(ScheduledMessage.AMQ_SCHEDULED_PERIOD, period);
        //msg.setIntProperty(ScheduledMessage.AMQ_SCHEDULED_REPEAT, repeat);
//           // Thread.sleep(1000);
        try {
            prod.send(msg);
        } catch (JMSException e) {
            e.printStackTrace();
            return Boolean.FALSE;
        }
        // 关闭资源
        sess.close();
        conn.close();
        return Boolean.TRUE;
    }

}
